package com.example.questionnaires.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnaires.common.Result;
import com.example.questionnaires.entity.ForumPost;
import com.example.questionnaires.entity.ForumComment;
import com.example.questionnaires.service.ForumService;
import com.example.questionnaires.vo.ForumPostVO;
import com.example.questionnaires.vo.ForumCommentVO;
import com.example.questionnaires.common.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/forum")
public class ForumController {
    @Autowired
    private ForumService forumService;

    @GetMapping("/posts")
    public Result<PageResult<ForumPostVO>> listPosts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return forumService.listPosts(page, size);
    }

    @GetMapping("/posts/{id}")
    public Result<ForumPostVO> getPost(@PathVariable Long id) {
        return forumService.getPost(id);
    }

    @PostMapping("/posts")
    public Result<Void> createPost(@RequestBody ForumPost post) {
        return forumService.createPost(post);
    }

    @DeleteMapping("/posts/{id}")
    public Result<Void> deletePost(@PathVariable Long id) {
        return forumService.deletePost(id);
    }

    @GetMapping("/posts/{postId}/comments")
    public Result<PageResult<ForumCommentVO>> listComments(
            @PathVariable Long postId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        return forumService.listComments(postId, page, size);
    }

    @PostMapping("/comments")
    public Result<Void> addComment(@RequestBody ForumComment comment) {
        return forumService.addComment(comment);
    }

    @DeleteMapping("/comments/{id}")
    public Result<Void> deleteComment(@PathVariable Long id) {
        return forumService.deleteComment(id);
    }
} 